var app = getApp();
const host = 'https://www.xiandu001.com/api'; // 服务器baseUrl
function postData(url, postData, doSuccess, doFail) {
  const token = wx.getStorageSync('userinfo').token;
  let params = Object.assign(postData, {token: token});
  wx.request({
    url: host + url,
    header: {
      'Content-Type': 'application/json',
      'Request-Origin': 'app'
    },
    data: params,
    method: 'POST',
    success: function (res) {
      // 参数值为res.data,直接将返回的数据传入
      // if (res.data.result.token) { // 如果有token保存下来，下次请求带着token访问
      // }
      if (!url.includes('get_shop_bytoken')) {
        if (res.statusCode == 401 | res.data.code == 200500) {
          wx.reLaunch({url: '/pages/authorize/authorize'})
        } else if (res.data.code != 200200) {
          wx.showToast({title: res.data.msg, icon: 'none', duration: 3000})
        }
      }

      doSuccess(res.data);
    },
    fail: function (fail) {
      wx.showToast({title: fail.data.msg, icon: 'none', duration: 3000})
      doFail();
      console.log(fail)
    }
  })
}

function getData(url, getData, doSuccess, doFail) {
  wx.request({
    url: host + url,
    header: {
      'Content-Type': 'application/json',
      'Request-Origin': 'app'
    },
    method: 'GET',
    data: getData,
    success: function (res) {
      doSuccess(res.data);
    },
    fail: function () {
      doFail();
    }
  })
}

module.exports.postData = postData;
module.exports.getData = getData;
